package 尽可能使字符串相等1208;

public class Solution {
    public int equalSubstring(String s, String t, int maxCost) {
        int cost = 0;
        int left = 0;
        int right = 0;
        int max = 0;
        while (right < s.length()) {
            cost += Math.abs(s.charAt(right) - t.charAt(right));
            if(cost > maxCost) {
                cost -= Math.abs(s.charAt(left) - t.charAt(left));
                if(left == right) {
                    right++;
                }else {
                    cost -= Math.abs(s.charAt(right) - t.charAt(right));
                }
                left++;
            }else {
                max = Math.max(max, right - left + 1);
                right++;
            }
        }
        while (left < s.length()) {
            if(cost > maxCost) {
                cost -= Math.abs(s.charAt(left) - t.charAt(left));
                left++;
            }else {
                max = Math.max(max, right - left);
                break;
            }
        }
        return max;
    }
}
